using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CoreCalc.SheetDefinedFunctions;
using Version=CoreCalc.SheetDefinedFunctions.Version;

namespace CoreCalc.GUI
{
    public partial class SDFControl : Form
    {
        private Workbook wb;

        public SDFControl(Workbook wb)
        {
            InitializeComponent();
            this.wb = wb;
            PopulateFunctionListBox();
            GetMarkValue();
        }

        private void GetMarkValue()
        {
            int mark = (int) SdfManager.MarkVersion;

            numericUpDown1.Value = mark;
        }

        private void PopulateFunctionListBox()
        {
            functionsListbox.Items.Clear();
            SdfInfo[] info = SdfManager.GetAllInfos();
            foreach (SdfInfo methodInfo in info)
            {
                functionsListbox.Items.Add(methodInfo.Show());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddSDF addBox = new AddSDF(wb);
            addBox.ShowDialog();
            PopulateFunctionListBox();
        }

        private void functionsListbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearInfo();
            if(functionsListbox.SelectedItem != null)
            {
                SdfInfo info = (SdfInfo) functionsListbox.SelectedItem;
                foreach (FullCellAddr cell in info.inputCells)
                {
                    inputCellsListBox.Items.Add(cell);
                }
                outPutCellTextBox.Text = info.outputCell.ToString();

            }
        }

        private void ClearInfo()
        {
            inputCellsListBox.Items.Clear();
            outPutCellTextBox.Text = string.Empty;
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            if(functionsListbox.SelectedItem != null)
            {
                SdfInfo info = (SdfInfo) functionsListbox.SelectedItem;
                SdfManager.DeleteMethod(info.name);
                PopulateFunctionListBox();
            }
        }

        private void dummyCreateButton_Click(object sender, EventArgs e)
        {
            int numbInputCells = (int)numbInputCellsUpDown.Value;
            Sheet sheet = null;
            bool funcSheetFound = false;

            for (int i = 0; i < wb.SheetCount && !funcSheetFound; i++)
            {
                sheet = wb[i];
                funcSheetFound = sheet.FunctionSheet;
            }
            if (!funcSheetFound)
                return;

            List<FullCellAddr> inputAddresses = new List<FullCellAddr>();

            for (int i = 0; i < numbInputCells; i++)
            {
                inputAddresses.Add(new FullCellAddr(i, 0, sheet));
            }
            FullCellAddr outputCell = new FullCellAddr(0, 3, sheet);

            SdfManager.DeleteMethod("TEST");
            SdfManager.CreateMethod("TEST", outputCell, inputAddresses);
            PopulateFunctionListBox();
        }

        private void ChangeStatus(int percent)
        {
            if (percent != -1)
            {
                progressBar1.Visible = true;
                progressBar1.Value = percent;
            }
            else
                progressBar1.Visible = false;
            
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            SetMarkValue();
            SdfManager.RegenerateAll();
        }

        private void SetMarkValue()
        {
            int value = (int) numericUpDown1.Value;

            Version ver = (Version) value;
            
            SdfManager.MarkVersion = ver;
            
        }
    }
}